package com.robmayhew.jsc;


import com.robmayhew.engine.spell.engine.SpellDictionary;
import com.robmayhew.engine.spell.engine.SpellDictionaryHashMap;
import com.robmayhew.engine.spell.event.SpellChecker;
import com.robmayhew.engine.spell.event.SpellCheckListener;
import com.robmayhew.engine.spell.event.SpellCheckEvent;


import java.io.*;
import java.util.ArrayList;


public class CheckFiles
        implements SpellCheckListener
{

    private SpellDictionary dictionary = null;
    private SpellChecker spellCheck = null;

    private ArrayList files = null;
    private static JavaFileFilter fileFilter = null;

    private GSettings gSettings = null;

    public void addWord(String word)
    {
        dictionary.addWord(word);
    }

   
    public CheckFiles()
    {
        gSettings = GSettings.getInstance();

        try
        {
            String dic = "eng.dic";
            
            InputStream stream = CheckFiles.class.getResourceAsStream(dic);
            if(stream == null)
                System.err.println("Could not find dictionary [" + dic + "]");
            InputStreamReader reader = new InputStreamReader(stream);
            if(reader == null)
                System.err.println("Could not open dictionary file");
            dictionary = new SpellDictionaryHashMap(reader);
            spellCheck = new SpellChecker(dictionary);
            reader.close();
            stream.close();
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            throw new RuntimeException("Error loading dictionary", t);
        }

    }

    public void checkFile(File file, Scan scan, boolean line)

    {
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(file));

            ArrayList lineList = new ArrayList();
            while (in.ready())
            {
                lineList.add((in.readLine()));

            }
            in.close();
            
            String[] lines = new String[lineList.size()];
            lineList.toArray(lines);
            JavaSrcWordTokenizer jswt = new JavaSrcWordTokenizer(lines);
            JavaSrcTokenList tokens = jswt.getTokens();
            for (int i = 0; i < tokens.size(); i++)
            {
                JavaSrcToken token = tokens.getToken(i);
                if (token.getToken().length() > gSettings.ignoreWordSize && token.getToken().length() < gSettings.ignoreWordOverSize)
                {
                    if (spellCheck.isCorrect(token.getToken()) == false)
                    {
                        write(token, file, scan, line);
                    }
                }
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    private void write(JavaSrcToken token, File file, Scan scan, boolean line)
    {
        Occurrence occ = new Occurrence();
        occ.setFile(file.getPath());
        occ.setCol(token.col);
        occ.setRow(token.row);
        occ.setWord(token.getToken());
        if(line)
            occ.setLine(token.getLine());
        occ.setSelectionStart(token.start);
        occ.setSelectionEnd(token.end);

        scan.add(occ);
    }

    public void spellingError(SpellCheckEvent event)
    {

    }

    private static void allFiles(File f, java.util.List list)
    {

        if (f.isDirectory())
        {
            File[] files = f.listFiles(fileFilter);
            for (int i = 0; i < files.length; i++)
            {
                allFiles(files[i], list);
            }
        }
        else
        {
            list.add(f);
        }
    }
    
    public static void main(String[] args){
        CheckFiles cf = new CheckFiles();
    }

}
